package edu.maping.easy.gui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.List;

import processing.core.PApplet;
import processing.core.PImage;
import controlP5.ControlP5;
import edu.maping.easy.ControlFacade;
import edu.maping.easy.io.KeyBoardInteraction;
import edu.maping.easy.model.EasyMappingPlugin;
import edu.maping.easy.model.MouseInteraction;
import edu.maping.easy.model.Thing;
import edu.maping.easy.model.World;
import fisica.FBody;
import fisica.FCircle;
import fisica.FWorld;
import fisica.Fisica;

public class GuiFactory implements EasyMappingPlugin, MouseInteraction {

	FWorld worldGUI = null;

	FBody clicked = null;
	FBody hovered = null;

	public int buttonColor = 0;
	public int hoverColor = 0;
	public int selectedColor = 0;
	public int selectedHoverColor = 0;

	PApplet pApplet;
	
	public FWorld getWorldGUI() {
		return worldGUI;
	}

	public FBody getClicked() {
		return clicked;
	}

	public void setClicked(FBody clicked) {
		this.clicked = clicked;
	}

	public FBody getHovered() {
		return hovered;
	}

	public FBody getHovered(int x, int y) {
		return worldGUI.getBody(x, y);
	}

	@Override
	public void start(World world, PApplet pApplet, ControlP5 controlP5) {
		// TODO Auto-generated method stub

	}

	@Override
	public void stop(World world, PApplet pApplet, ControlP5 controlP5) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setup(PApplet pApplet) {
		Fisica.init(pApplet);
		worldGUI = new FWorld(pApplet.width,pApplet.height);

		buttonColor = pApplet.color(122, 122, 200);
		hoverColor = pApplet.color(122, 255, 255);
		selectedColor = pApplet.color(255, 125, 125);
		selectedHoverColor = pApplet.color(255, 180, 125);

		this.pApplet = pApplet;

	}

	@Override
	public void setup(PApplet pApplet, GuiFactory guiFactory) {
		// TODO Auto-generated method stub

	}

	@Override
	public void draw(PApplet pApplet) {
		worldGUI.step();
		worldGUI.draw();
	}

	@Override
	public void setParameters(Thing thing) {
		// TODO Auto-generated method stub

	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void mouseDragged(int mouseX, int mouseY, Thing newThing) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean mouseClicked(int mouseX, int mouseY, Thing newThing,
			MouseEvent mouseEvent) {
		return false;
	}

	@Override
	public void mouseReleased(int mouseX, int mouseY, Thing newThing) {
		List<FBody> bodies = worldGUI.getBodies(mouseX, mouseY);
		if (!bodies.isEmpty()) {
			clicked = bodies.get(0);
		}
	}

	@Override
	public void mousePreassed(int mouseX, int mouseY, Thing newThing) {
		// TODO Auto-generated method stub

	}

	@Override
	public void editKeyPressed(int key, Thing thing) {
		// TODO Auto-generated method stub

	}

	@Override
	public void KeyPressed(KeyBoardInteraction keyBoardInteraction) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		// TODO Auto-generated method stub

	}

	public void add(FBody body) {

		body.setFillColor(buttonColor);
		worldGUI.add(body);
	}

	@Override
	public void mouseMoved(int mouseX, int mouseY) {

		hovered = worldGUI.getBody(mouseX, mouseY);

		if (hovered != null && hovered.isDrawable()) {
			pApplet.cursor(PApplet.CROSS);
			if (hovered instanceof Button) {
				Button button = (Button) hovered;
				int color = button.getColor();
				if (color == selectedColor) {
					hovered.setFillColor(selectedHoverColor);
				} else if (color == buttonColor) {
					hovered.setFillColor(hoverColor);
				}
			}
			if (hovered instanceof CircleButton) {
				CircleButton button = (CircleButton) hovered;
				int color = button.getColor();
				button.setSize(button.sizeHover);
				button.setTextColor(pApplet.color(255, 255, 0));
				if (color == selectedColor) {
					hovered.setFillColor(selectedHoverColor);
				} else if (color == buttonColor) {
					hovered.setFillColor(hoverColor);
				}
			}
		} else {
			if (pApplet.getCursor().getType() == PApplet.CROSS)
				pApplet.cursor(PApplet.ARROW);
		}

		for (int index = 0; index < worldGUI.getBodies().size(); index++) {
			FBody body = (FBody) worldGUI.getBodies().get(index);

			if (body != hovered) {
				if (body instanceof Button) {
					Button button = (Button) body;
					int color = button.getColor();
					if (color == hoverColor) {
						button.setFillColor(buttonColor);
					} else if (color == selectedHoverColor) {
						button.setFillColor(selectedColor);
					}
				} else if (body instanceof CircleButton) {
					CircleButton button = (CircleButton) body;
					button.setSize(button.realSize);
					button.setTextColor(255);
					int color = button.getColor();
					if (color == hoverColor) {
						button.setFillColor(buttonColor);
					} else if (color == selectedHoverColor) {
						button.setFillColor(selectedColor);
					}

				} else {
					body.setFillColor(buttonColor);
				}

			}
		}
	}

	public ButtonList newButtonList(String name, float x, float y, int width,
			int height) {
		ButtonList buttonList = new ButtonList(worldGUI, name, x, y, width,
				height, pApplet);
		add(buttonList);
		return buttonList;
	}

	public Slider newSlider(String name, float width, float height,
			float value, float maxValue, float minValue) {
		Slider slider = new Slider(name, width, height, value, maxValue,
				minValue);
		slider.setStatic(true);
		slider.setGrabbable(false);
		add(slider);
		return slider;
	}

	public Button newButton(String name, float x, float y, int width, int height) {
		Button button = new Button(name, x, y, width, height, pApplet);
		add(button);
		return button;
	}

	public CircleButton newCircleButton(String name, float x, float y, int size) {
		CircleButton button = new CircleButton(name, x, y, size, pApplet);
		add(button);
		return button;
	}

	public CircleButton newCircleButton(CircleButton circleButton) {
		CircleButton button = new CircleButton(circleButton.name, circleButton
				.getX(), circleButton.getY(), (int) circleButton.getSize(),
				pApplet);
		add(button);
		return button;
	}
}
